classdef connectome
    % definition of class connectome
    properties
        numberOfLayers;
        numberOfNeuronsInLayer;
        neurons; %matrix of neurons
        synapses; %matrix of synapse
    end
    
    methods
        %constructor
        function obj = connectome(numberOfLayers, numberOfNeuronsInLayer)
            counter = 1;
            for i=1:numberOfLayers
                for j=1:numberOfNeuronsInLayer
                    obj.neurons(1,counter) = neuron(j,i,0.5); %we define
                    counter = counter+1;
                end
            end
            counter = 1;
            for i=1:(numberOfLayers-1)
                for j=1:numberOfNeuronsInLayer
                    for k=1:numberOfNeuronsInLayer
                        obj.synapses(1,counter) = synapse(1,10,j,i,k,i+1);
                        counter = counter +1;
                    end
                end
            end
            obj.numberOfLayers = numberOfLayers;
            obj.numberOfNeuronsInLayer = numberOfNeuronsInLayer;
        end

        function obj = randomizeSynapsesWeights(obj)
            for i=1:length(obj.synapses)
                obj.synapses(1,i).weight = rand;
            end
        end
        function obj = randomizeSynapsesRechargingTimes(obj,lower_border,upper_border)
            for i=1:length(obj.synapses)
                obj.synapses(1,i).definedRechargingTime = lower_border + rand*(upper_border-lower_border);
            end
        end
        function obj = decreaseRemainingTime(obj)
            for i=1:length(obj.synapses)
                if(obj.synapses(1,i).remainingTime >0)
                    obj.synapses(1,i).remainingTime = obj.synapses(1,i).remainingTime - 1;
                end
            end
        end
        
        function syn = getSynapse(obj, neuronInId,neuronInLayer,neuronOutId,neuronOutLayer)
            for i=1:length(obj.synapses)
                if((obj.synapses(1,i).inputNeuronId == neuronInId) && (obj.synapses(1,i).inputNeuronLayer == neuronInLayer) && (obj.synapses(1,i).outputNeuronId == neuronOutId) && (obj.synapses(1,i).outputNeuronLayer == neuronOutLayer))
                    syn = obj.synapses(1,i);
                    break;
                end
            end
        end
        function neu = getNeuron(obj, neuronId,neuronLayer)
            for i=1:length(obj.neurons)
                if((obj.neurons(1,i).numberInLayer == neuronId) && (obj.synapses(1,i).layer == neuronLayer))
                    neu = obj.neurons(1,i);
                    break;
                end
            end
        end
        
        
        
        function output = computeSpikesMatrix(obj, input)
            usedInput = input;
            usedOutput = zeros(size(input));
            
            usedOutput = usedInput;
            matrixOfSpikes = zeros(length(input),obj.numberOfLayers);
            matrixOfSpikes(:,1) = usedOutput';
            for i=2:obj.numberOfLayers
                for j=1:obj.numberOfNeuronsInLayer
                    neuronIn = getNeuron(obj,i,j);
                    value = 0;
                    for k=1:obj.numberOfNeuronsInLayer
                       synapse = getSynapse(obj,k,i-1,j,i);
                       value = value + PassSignal(synapse,usedOutput(j));
                    end
                    if(value > neuronIn.threshold)
                        usedOutput(j) = 1;
                    else
                        usedOutput(j) = 0;
                    end
                end
                matrixOfSpikes(:,i) = usedOutput';
            end
            outputusedOutput;
        end
    end
    
end

